package com.st.listener;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

@WebListener
public class SessionAttrListener implements HttpSessionAttributeListener {

    public void attributeAdded(HttpSessionBindingEvent se)  { 
    	// 属性添加
    	System.out.println("属性添加");
    	// 添加了一个用户
    	if("user".equals(se.getName())) {
    		// 获取app
    		ServletContext app = se.getSession().getServletContext();
    		
    		int loginCount = (int) app.getAttribute("loginCount");
    		loginCount ++;
    		app.setAttribute("loginCount", loginCount);
    	}
    }

    public void attributeRemoved(HttpSessionBindingEvent se)  { 
    	// 属性移除
    	System.out.println("属性移除");
    	// 添加了一个用户
    	if("user".equals(se.getName())) {
    		// 获取app
    		ServletContext app = se.getSession().getServletContext();
    		
    		int loginCount = (int) app.getAttribute("loginCount");
    		loginCount --;
    		app.setAttribute("loginCount", loginCount);
    	}
    }

    public void attributeReplaced(HttpSessionBindingEvent se)  { 
    	// 属性替换
    	System.out.println("替换");
    }
	
}
